App.popup = function(args){
	var opts = _.extend({
		id: 'default_popup',
		html: '',
		zIndex: 10,
		mask: true,
		duration: 0,
		callback: function(){},
		closeCallback: function(){},
	}, args);

	opts.zIndex = parseInt(opts.zIndex);
	opts.duration = parseInt(opts.duration);

	App.popup.zIndex = App.popup.zIndex ? (App.popup.zIndex + 1) :  zIndex;

	var $mask = ($('#overlay').length > 0) ? $('#overlay') : $('<div id="overlay"></div>').prependTo('#windows'),
		$popup = ($('#' + opts.id).length > 0)  ? $('#' + opts.id) : $('<div id="'+ opts.id +'" class="popup"></div>').prependTo('#windows');

	$popup.html(opts.html);

	opts.mask && $mask.attr({target: 'target'});

	$popup.attr({
		target: 'target'
	}).css({
		zIndex: App.popup.zIndex
	});

	if( opts.duration > 0){
		var a = _.delay(function(){
			App.popup.close({
				id: opts.id
			});

			opts.closeCallback();
		}, opts.duration)
	}
}

App.popup.close = function(args){
	args = args || {};
	args.id = args.id || 'default_popup';

	var $popup = $('#' + args.id),
		$mask = $('#overlay');
	if($popup.length > 0){
		$mask.attr({target: ''});

		$popup.attr({target: ''})
	}
}

App.popup.zIndex = 100;